//
//  DCChangeBankCardController.m
//  Demo
//
//  Created by zhaolin on 2023/1/5.
//

#import "DCChangeBankCardController.h"
#import "DCChengeBankView.h"
#import "DCBankCardModel.h"
#import "DCChangeBankSmsView.h"
#import "DCBankModel.h"


@interface DCChangeBankCardController ()<DCChengeBankViewDelegate>

@property (nonatomic, strong) DCChengeBankView *bankView;
@property (nonatomic, strong) DCChengeBankView *bankAccoutView;
@property (nonatomic, strong) DCChengeBankView *bankAccoutSureView;



@property (nonatomic, strong) DCChangeBankSmsView *smsView;

@property (nonatomic, strong) NSMutableArray *bankArr;

@end

@implementation DCChangeBankCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleName:@"Change bank card"];
    [self getDataFromNet];
    // Do any additional setup after loading the view.
}
- (void)configureUI {
    [super configureUI];
    DCChengeBankView *view = [[DCChengeBankView alloc] init];
    _bankView = view;
    [view configureTitle:@"Bank"];
    [view configureContent:self.cardModel.data.bankName];
    view.delegate = self;
    view.canEdit = NO;
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.navgitBar.mas_bottom);
        make.height.mas_equalTo(105);
    }];
    
    DCChengeBankView *view2 = [[DCChengeBankView alloc] init];
    _bankAccoutView = view2;
    [view2 configureTitle:@"Bank Account"];
    [view2 configureContent:self.cardModel.data.bankCard];
    view2.delegate = self;
    view2.canEdit = YES;
    [self.view addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(view.mas_bottom);
        make.height.mas_equalTo(105);
    }];
    
    DCChengeBankView *view3 = [[DCChengeBankView alloc] init];
    _bankAccoutSureView = view3;
    [view3 configureTitle:@"Confirm Bank Account"];
    view3.delegate = self;
    view3.canEdit = YES;
    [self.view addSubview:view3];
    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(view2.mas_bottom);
        make.height.mas_equalTo(105);
    }];
    
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    commitBtn.backgroundColor = [UIColor colorWithString:@"#1C221B"];
    commitBtn.layer.cornerRadius =8;
    commitBtn.layer.masksToBounds = YES;
    [commitBtn setTitle:@"Submit" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor colorWithString:@"#FFFFFF"] forState:UIControlStateNormal];
    commitBtn.titleLabel.font = DCFont_PF_Semibold(15);
    [commitBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(38);
        make.right.mas_equalTo(-38);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(view3.mas_bottom).mas_offset(45);
    }];
}

- (void) getDataFromNet {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DCHttpRequest DCBankListCompleteBlock:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (responseObject.code == 200) {
            NSArray *dataArr = [DCBankModel mj_objectArrayWithKeyValuesArray:[responseObject.data objectForKey:@"banks"]];
            [self.bankArr addObjectsFromArray:dataArr];
        } else {
            [DCDCToast toastText:responseObject.message];
        }
    }];
}
- (void)showCodeAlert {
    DLog(@"%@",self);
    _smsView = [[DCChangeBankSmsView alloc] initWithFrame:CGRectZero];
    _smsView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_smsView];
    [_smsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
}
#pragma mark - DCChengeBankViewDelegate
- (void)endEditingWithText:(NSString *)text {
    if (text.length) {
        [self showCodeAlert];
    }
}

#pragma mark - DCChangeBankSmsViewDelegate
- (void)changeBankSmsViewCloseAction:(DCChangeBankSmsView *)view {
    [_smsView  setHidden:YES];
}

- (void)changeBankSmsViewSendCodeAction:(DCChangeBankSmsView *)view {
    //register login
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    DCUserInfo *userInfo = [[CacheTools sharedCacheTools] getObjForKey:SMARTUSERINFO];
    [DCHttpRequest DCgetSmsWithPhoneNum:userInfo.phone registerType:@"bank" completeBlock:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (responseObject.code == 200) {
            [self.smsView sendingVCodel];
            [DCDCToast toastText:@"【SMS verification code obtained successfully】"];
        } else {
            [DCDCToast toastText:responseObject.message];
        }
    }];

}

- (void)changeBankSmsViewCommitActionwithText:(NSString *)vCodel view:(DCChangeBankSmsView *)view {
    
}

- (void)commitAction:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"Next"]) {
        if ([_bankAccoutView.content isEqualToString:_bankAccoutSureView.content]) {
            [self showCodeAlert];
            [sender setTitle:@"Submit" forState:UIControlStateNormal];
        } else {
            [DCDCToast toastText:@"【Please ensure that the card number entered twice is the same.】"];
        }

    } else {
        
        [DCHttpRequest DCmodifyBankcard:@"" step:@"" vcode:@"" CompleteBlock:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
            if (responseObject.code == 200) {
                [DCDCToast toastText:@"【Your bank account has been successfully modified!】"];
            } else {
                [DCDCToast toastText:responseObject.message];
            }
        }];
    }
    //
}


@end
